﻿using System;
using System.Collections.Generic;
using System.Text;
using CLoggerNamespace;

namespace HL7v_2x
{
    public class HL7SegmentPV2 : IHL7Segment
    {

        #region DESARMAR SEGMENTO EN CAMPOS
        public List<HL7Fields> DesarmarSegmentoEnCampos(string strSegment)
        {
           
            List<HL7Fields> lstHL7Fields = new List<HL7Fields>();
            //EJEMPLO MENSAJE HL7
            /*               
              
            */
            string[] strArrAux = new string[] { };

            try
            {
                strArrAux = strSegment.Split('|');
                int inCantBarras = strArrAux.Length - 1;
                CLogger.Append("Cantidad de Barras: " + inCantBarras + " - MAXIMO DE 50 CAMPOS.", CLogger.INFO);
            }
            catch (Exception ex)
            {
                CLogger.Append("ERROR PARCEANDO LAS BARRAS | Ex: " + ex.Message, CLogger.INFO);
                throw ex;
            }

            try
            {
                if ((strArrAux.Length > 0) && (strArrAux[0] != null) && (strArrAux[0] != ""))    // SetId_PV1 = 00131,        
                {
                    CLogger.Append("Field 0: " + strArrAux[0], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PriorPendingLocation_PV2, enc.GetBytes(strArrAux[0])));
                }
                if ((strArrAux.Length > 1) && (strArrAux[1] != null) && (strArrAux[1] != ""))  // PatientClass_PV1 = 00132,
                {
                    CLogger.Append("Field 1: " + strArrAux[1], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.AccommodationCode_PV2, enc.GetBytes(strArrAux[1])));
                }
                if ((strArrAux.Length > 2) && (strArrAux[2] != "") && (strArrAux[2] != null)) // AssignedPatientLocation_PV1 = 00133,
                {
                    CLogger.Append("Field 2: " + strArrAux[2], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.AdmitReason_PV2, enc.GetBytes(strArrAux[2])));
                }
                if ((strArrAux.Length > 3) && (strArrAux[3] != "") && (strArrAux[3] != null)) // AdmisionType_PV1 = 00134,
                {
                    CLogger.Append("Field 3: " + strArrAux[3], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.TransferReason_PV2, enc.GetBytes(strArrAux[3])));
                }
                if ((strArrAux.Length > 4) && (strArrAux[4] != "") && (strArrAux[4] != null)) // PreadmitNumber_PV1 = 00135,
                {
                    CLogger.Append("Field 4: " + strArrAux[4], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientValuables_PV2, enc.GetBytes(strArrAux[4])));
                }
                if ((strArrAux.Length > 5) && (strArrAux[5] != "") && (strArrAux[5] != null)) // PriodPatientLocation_PV1=00136,
                {
                    CLogger.Append("Field 5: " + strArrAux[5], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientValuablesLocation_PV2, enc.GetBytes(strArrAux[5])));
                }
                if ((strArrAux.Length > 6) && (strArrAux[6] != "") && (strArrAux[6] != null)) //AttendingDoctor_PV1 = 00137,
                {
                    CLogger.Append("Field 6: " + strArrAux[6], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.VisitUserCode_PV2, enc.GetBytes(strArrAux[6])));
                }

                if ((strArrAux.Length > 7) && (strArrAux[7] != "") && (strArrAux[7] != null)) //ReferingDoctor_PV1 = 00138,
                {
                    CLogger.Append("Field 7: " + strArrAux[7], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ExpectedAdmitDateTime_PV2, enc.GetBytes(strArrAux[7])));
                }
                if ((strArrAux.Length > 8) && (strArrAux[8] != "") && (strArrAux[8] != null)) //   ConsultingDoctor_PV1 = 00139,
                {
                    CLogger.Append("Field 8: " + strArrAux[8], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ExpectedDischargeDateTime_PV2, enc.GetBytes(strArrAux[8])));
                }
                if ((strArrAux.Length > 9) && (strArrAux[9] != "") && (strArrAux[9] != null)) //  HospitalService_PV1 = 00140,
                {
                    CLogger.Append("Field 9: " + strArrAux[9], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.EstimatedLengthofInpatientStay_PV2, enc.GetBytes(strArrAux[9])));
                }
                if ((strArrAux.Length > 10) && (strArrAux[10] != "") && (strArrAux[10] != null)) //    TemporaryLocation_PV1 = 00141,
                {
                    CLogger.Append("Field 10: " + strArrAux[10], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ActualLengthofInpatientStay_PV2, enc.GetBytes(strArrAux[10])));
                }

                if ((strArrAux.Length > 11) && (strArrAux[11] != "") && (strArrAux[11] != null)) //    PreadmitTestIndicator_PV1 = 00142,
                {
                    CLogger.Append("Field 11: " + strArrAux[11], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.VisitDescription_PV2, enc.GetBytes(strArrAux[11])));
                }
                if ((strArrAux.Length > 12) && (strArrAux[12] != "") && (strArrAux[12] != null)) //    ReAdmissionIndicator_PV1 = 00143,
                {
                    CLogger.Append("Field 12: " + strArrAux[12], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ReferralSourceCode_PV2, enc.GetBytes(strArrAux[12])));
                }
                if ((strArrAux.Length > 13) && (strArrAux[13] != "") && (strArrAux[13] != null)) //   AdmitSource_PV1 = 00144,
                {
                    CLogger.Append("Field 13: " + strArrAux[13], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PreviousServiceDate_PV2, enc.GetBytes(strArrAux[13])));
                }
                if ((strArrAux.Length > 14) && (strArrAux[14] != "") && (strArrAux[14] != null)) //    AmbulatoryStatus_PV1_NK1 = 00145,
                {
                    CLogger.Append("Field 14: " + strArrAux[14], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.EmploymentIllnessRelatedIndicator_PV2, enc.GetBytes(strArrAux[14])));
                }

                if ((strArrAux.Length > 15) && (strArrAux[15] != "") && (strArrAux[15] != null)) //   VIPIndicator_PV1 = 00146,
                {
                    CLogger.Append("Field 15: " + strArrAux[15], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PurgeStatusCode_PV2, enc.GetBytes(strArrAux[15])));
                }
                if ((strArrAux.Length > 16) && (strArrAux[16] != "") && (strArrAux[16] != null)) //   AdmittingDoctor_PV1 = 00147,
                {
                    CLogger.Append("Field 16: " + strArrAux[16], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PurgeStatusDate_PV2, enc.GetBytes(strArrAux[16])));
                }
                if ((strArrAux.Length > 17) && (strArrAux[17] != "") && (strArrAux[17] != null)) //    PatientType_PV1 = 00148,
                {
                    CLogger.Append("Field 17: " + strArrAux[17], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.SpecialProgramCode_PV2, enc.GetBytes(strArrAux[17])));
                }
                if ((strArrAux.Length > 18) && (strArrAux[18] != "") && (strArrAux[18] != null)) //   VisitNumber_PV1 = 00149,
                {
                    CLogger.Append("Field 18: " + strArrAux[18], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.RetentionIndicator_PV2, enc.GetBytes(strArrAux[18])));
                }

                if ((strArrAux.Length > 19) && (strArrAux[19] != "") && (strArrAux[19] != null)) //   FinancialClass_PV1 = 00150,
                {
                    CLogger.Append("Field 19: " + strArrAux[19], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ExpectedNumberofInsurancePlans_PV2, enc.GetBytes(strArrAux[19])));
                }
                if ((strArrAux.Length > 20) && (strArrAux[20] != "") && (strArrAux[20] != null)) //   ChargePriceIndicator_PV1 = 00151,
                {
                    CLogger.Append("Field 20: " + strArrAux[20], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.VisitPublicityCode_PV2, enc.GetBytes(strArrAux[20])));
                }
                if ((strArrAux.Length > 21) && (strArrAux[21] != "") && (strArrAux[21] != null)) //   CourtesyCode_PV1 = 00152,
                {
                    CLogger.Append("Field 21: " + strArrAux[21], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.VisitProtectionIndicator_PV2, enc.GetBytes(strArrAux[21])));
                }
                if ((strArrAux.Length > 22) && (strArrAux[22] != "") && (strArrAux[22] != null)) //   CreditRating_PV1 = 00153,
                {
                    CLogger.Append("Field 22: " + strArrAux[22], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ClinicOrganizationName_PV2, enc.GetBytes(strArrAux[22])));
                }

                if ((strArrAux.Length > 23) && (strArrAux[23] != "") && (strArrAux[23] != null)) //   ContractCode_PV1 = 00154,
                {
                    CLogger.Append("Field 23: " + strArrAux[23], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientStatusCode_PV2, enc.GetBytes(strArrAux[23])));
                }
                if ((strArrAux.Length > 24) && (strArrAux[24] != "") && (strArrAux[24] != null)) //   ContractEffectiveDate_PV1 = 00155,
                {
                    CLogger.Append("Field 24: " + strArrAux[24], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.VisitPriorityCode_PV2, enc.GetBytes(strArrAux[24])));
                }
                if ((strArrAux.Length > 25) && (strArrAux[25] != "") && (strArrAux[25] != null)) //  ContractAmount_PV1 = 00156,
                {
                    CLogger.Append("Field 25: " + strArrAux[25], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PreviousTreatmentDate_PV2, enc.GetBytes(strArrAux[25])));
                }
                if ((strArrAux.Length > 26) && (strArrAux[26] != "") && (strArrAux[26] != null)) //   ContractPeriod_PV1 = 00157,
                {
                    CLogger.Append("Field 26: " + strArrAux[26], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ExpectedDischargeDisposition_PV2, enc.GetBytes(strArrAux[26])));
                }
                if ((strArrAux.Length > 27) && (strArrAux[27] != "") && (strArrAux[27] != null)) //   InterestCode_PV1 = 00158,
                {
                    CLogger.Append("Field 27: " + strArrAux[27], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.SignatureonFileDate_PV2, enc.GetBytes(strArrAux[28])));
                }
                if ((strArrAux.Length > 28) && (strArrAux[28] != "") && (strArrAux[28] != null)) // TransfertoBadDebtCode_PV1 = 00159,
                {
                    CLogger.Append("Field 28: " + strArrAux[28], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.FirstSimilarIllnessDate_PV2, enc.GetBytes(strArrAux[28])));
                }

                if ((strArrAux.Length > 29) && (strArrAux[29] != "") && (strArrAux[29] != null)) //TransfertoBadDebtDate_PV1 = 00160,
                {
                    CLogger.Append("Field 29: " + strArrAux[29], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientChargeAdjustmentCode_PV2, enc.GetBytes(strArrAux[29])));
                }
                if ((strArrAux.Length > 30) && (strArrAux[30] != "") && (strArrAux[30] != null)) // BadDebtAgencyCode_PV1 = 00161,
                {
                    CLogger.Append("Field 30: " + strArrAux[30], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.RecurringServiceCode_PV2, enc.GetBytes(strArrAux[30])));
                }
                if ((strArrAux.Length > 31) && (strArrAux[31] != "") && (strArrAux[31] != null)) // BadDebtTransferAmount_PV1 = 00162,
                {
                    CLogger.Append("Field 31: " + strArrAux[31], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.BillingMediaCode_PV2, enc.GetBytes(strArrAux[31])));
                }
                if ((strArrAux.Length > 32) && (strArrAux[32] != "") && (strArrAux[32] != null)) // BadDebtRecoveryAmount_PV1 = 00163,
                {
                    CLogger.Append("Field 32: " + strArrAux[32], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ExpectedSurgeryDateandTime_PV2, enc.GetBytes(strArrAux[32])));
                }

                if ((strArrAux.Length > 33) && (strArrAux[33] != "") && (strArrAux[33] != null)) //DeleteAccountIndicator_PV1 = 00164,
                {
                    CLogger.Append("Field 33: " + strArrAux[33], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.MilitaryPartnershipCode_PV2, enc.GetBytes(strArrAux[30])));
                }
                if ((strArrAux.Length > 34) && (strArrAux[34] != "") && (strArrAux[34] != null)) // DeleteAccountDate_PV1=00165,
                {
                    CLogger.Append("Field 34: " + strArrAux[34], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.MilitaryNonAvailabilityCode_PV2, enc.GetBytes(strArrAux[34])));
                }
                if ((strArrAux.Length > 35) && (strArrAux[35] != "") && (strArrAux[35] != null)) // DischargeDisposition_PV1 = 00166,
                {
                    CLogger.Append("Field 35: " + strArrAux[35], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.NewbornBabyIndicator_PV2, enc.GetBytes(strArrAux[35])));
                }

                if ((strArrAux.Length > 36) && (strArrAux[36] != "") && (strArrAux[36] != null)) //     DischargedtoLocation_PV1 = 00167,
                {
                    CLogger.Append("Field 36: " + strArrAux[36], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.BabyDetainedIndicator_PV2, enc.GetBytes(strArrAux[36])));
                }
                if ((strArrAux.Length > 37) && (strArrAux[37] != "") && (strArrAux[37] != null)) // DietType_PV1 = 00168,
                {
                    CLogger.Append("Field 37: " + strArrAux[37], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ModeofArrivalCode_PV2, enc.GetBytes(strArrAux[37])));
                }
                if ((strArrAux.Length > 38) && (strArrAux[38] != "") && (strArrAux[38] != null)) //    ServicingFacility_PV1=00169,
                {
                    CLogger.Append("Field 38: " + strArrAux[38], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.RecreationalDrugUseCode_PV2, enc.GetBytes(strArrAux[38])));
                }

                if ((strArrAux.Length > 39) && (strArrAux[39] != "") && (strArrAux[39] != null)) //   AccountStatus_PV1 = 00171,
                {
                    CLogger.Append("Field 39: " + strArrAux[39], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.AdmissionLevelofCareCode_PV2, enc.GetBytes(strArrAux[39])));
                }
                if ((strArrAux.Length > 40) && (strArrAux[40] != "") && (strArrAux[40] != null)) //  PendingLocation_PV1 = 00172,
                {
                    CLogger.Append("Field 40: " + strArrAux[40], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PrecautionCode_PV2, enc.GetBytes(strArrAux[40])));
                }
                if ((strArrAux.Length > 41) && (strArrAux[41] != "") && (strArrAux[41] != null)) //    PriorTemporaryLocation_PV1 = 00173,
                {
                    CLogger.Append("Field 41: " + strArrAux[41], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientConditionCode_PV2, enc.GetBytes(strArrAux[41])));
                }

                if ((strArrAux.Length > 42) && (strArrAux[42] != "") && (strArrAux[42] != null)) //   AdmitDate_Time_PV1 = 00174,
                {
                    CLogger.Append("Field 42: " + strArrAux[42], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.LivingWillCode_PV2, enc.GetBytes(strArrAux[42])));
                }
                if ((strArrAux.Length > 43) && (strArrAux[43] != "") && (strArrAux[43] != null)) //  DischargeDateTime_PV1 = 00175,
                {
                    CLogger.Append("Field 43: " + strArrAux[43], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.OrganDonorCode_PV2, enc.GetBytes(strArrAux[43])));
                }
                if ((strArrAux.Length > 44) && (strArrAux[44] != "") && (strArrAux[44] != null)) //   CurrentPatientBalance_PV1 = 00176,
                {
                    CLogger.Append("Field 44: " + strArrAux[44], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.AdvanceDirectiveCode_PV2, enc.GetBytes(strArrAux[44])));
                }

                if ((strArrAux.Length > 45) && (strArrAux[45] != "") && (strArrAux[45] != null)) //   TotalCharges_PV1 = 00177,
                {
                    CLogger.Append("Field 45: " + strArrAux[45], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientStatusEffectiveDate_PV2, enc.GetBytes(strArrAux[45])));
                }
                if ((strArrAux.Length > 46) && (strArrAux[46] != "") && (strArrAux[46] != null)) //  TotalAdjustments_PV1 = 00178,
                {
                    CLogger.Append("Field 46: " + strArrAux[46], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ExpectedLOAReturnDateTime_PV2, enc.GetBytes(strArrAux[46])));
                }
                
            }
            catch (Exception ex)
            {
                                
                throw new Exception("EXCEPCION AL DESCOMPONER SEGMENTO PV2 - Ex: "+ex.Message);
            }

            CLogger.Append("**********************FINAL DEL DESCOMPONER SEGMENTO PV2 OK*******************", CLogger.INFO);

              return lstHL7Fields;

          }
        #endregion


    //    //public HL7SegmentPV2()
    //    //    : base(HL7MessageName.MessagePV2)
    //    //{

    //    //}

    //    //public int BuildFields(byte[] byteInputStreamParam)
    //    //{
    //    //    throw new NotImplementedException();
    //    //}

    //    //public int BuildFields(byte[] byteInputStreamParam, int intOffset)
    //    //{
    //    //    throw new NotImplementedException();
    //    //}
    }
}
